﻿using System;

using Sunyi.Project.WebPoint.DataAccess;
using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccessController
{
    public abstract class ProvisioningDaoController<Entity, Dao> : BaseDaoController<Entity, Dao>
        where Entity : class, IDataEntity
        where Dao : IDao<Entity>, new()
    {
        public override Entity Select(Guid id)
        {
            Entity entity = CacheProvider.Instance.Get<Entity>(id);
            if (entity == null)
            {
                entity = dao.Select(id);
                if (entity != null) { this.FillEntity(entity); CacheProvider.Instance.Add<Entity>(entity.Id, entity); }
            }

            return entity;
        }

        public override void Update(Entity entity)
        {
            dao.Update(entity);

            CacheProvider.Instance.Remove<Entity>(entity.Id);
        }

        public override void Delete(Guid id)
        {
            dao.Delete(id);

            CacheProvider.Instance.Remove<Entity>(id);
        }
    }
}
